using System;
using System.Threading;
using System.Runtime.InteropServices;

class App {
   static void Main() {
      Thread t = new Thread(new ThreadStart(ThreadFunc));
      t.Start();
      Thread.Sleep(2000);
      t.Interrupt();
      t.Join();
      Console.WriteLine("Main thread: The new thread terminted");
      Console.ReadLine();
   }

   static void ThreadFunc() {
      Console.WriteLine("Thread is starting");
      try {
         if (0 == SleepEx(5000, true)) {
            Console.WriteLine("Thread timed-out");
            Thread.Sleep(10000);
            Console.WriteLine("after sleep");
         } else {
            Console.WriteLine("Thread processed APC");
         }
      }
      catch (ThreadInterruptedException e){
         Console.WriteLine("Caught ThreadInterruptedException");
      }
      Console.WriteLine("Thread is dying");
   }

   [DllImport("Kernel32.dll")]
   static extern Int32 SleepEx(UInt32 timeout, Boolean fAlertable);
}